package com.ujoku.service.impl;

import com.labillusion.core.database.base.mybatis.dao.BaseDao;
import com.labillusion.core.database.base.mybatis.service.base.impl.BaseServiceImpl;
import com.labillusion.core.platform.exception.ResourceNotFoundException;
import com.labillusion.core.util.StringUtils;
import com.ujoku.context.ActionContext;
import com.ujoku.dao.ArticleDao;
import com.ujoku.dao.CartDao;
import com.ujoku.domain.*;
import com.ujoku.service.ArticleService;
import com.ujoku.service.CartItemService;
import com.ujoku.service.CartService;
import com.ujoku.service.CdnService;
import com.ujoku.service.admin.ParametersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 14-10-19.
 */
@Service
public class CartServiceImpl extends BaseServiceImpl<Cart> implements CartService<Cart> {

    @Autowired
    private CartDao dao;

    @Autowired
    private CartItemService cartItemService;


    @Override
    protected BaseDao<Cart> getBaseDao() {
        return dao;
    }

    @Autowired
    private CdnService cdnService;

    @Autowired
    private ParametersService parametersService;

    @Override
    public Cart getCart(String visitorId){

        Map<String,Object> map = new HashMap<>();
        map.put("visitorId" , visitorId);

        Cart cart = this.selectOne(map);

        if(cart != null)
        {
            List<CartItem> cartItemList =  cartItemService.getCartIemByCartId(cart.getId());
            cartItemList.forEach(i->i.setSmallImage(cdnService.buildGoodsImage(i.getGoodsId(), CdnService.Goods_Small, i.getGoodsUpdateTime().getTime())));
            cart.setCartItemList(cartItemList);
        }

        return cart;
    }

    @Override
    public void calculateTotal(Cart cart) {
        if(cart == null)
            return;

        if(cart.getCartItemList() == null || cart.getCartItemList().size() == 0)
            return;


        Double subTotal = 0d;
        Double goodPriceTotal = 0d;
        for(CartItem item : cart.getCartItemList()){
            //如果是私人酒窖的item, 价格是就是price
            item.setSubTotal(StringUtils.hasText(item.getMyListItemId())?item.getPrice() * item.getQty() :item.getGoodsPrice() * item.getQty());

            item.setTotal(item.getPrice() * item.getQty());

            goodPriceTotal += item.getSubTotal();
            subTotal +=  item.getTotal();
        }

        cart.setSubTotal(subTotal);
        cart.setDiscount(goodPriceTotal-subTotal);

        if(cart.getDeliveryType() != null && DeliveryType.EXPRESS ==  cart.getDeliveryType()){
            //暂时默认给10元
            cart.setShippingFee(calculateShippingFee(cart));
            cart.setTotal(cart.getShippingFee() + cart.getSubTotal());
        }
        else{
            cart.setShippingFee(0d);
            cart.setTotal(cart.getSubTotal());
        }
    }

    public Double calculateShippingFee(Cart cart){
        Double shippingFee = Double.valueOf(parametersService.getParameterByKey("shippingFee").getValue());
        Double remoteShippingFee = Double.valueOf(parametersService.getParameterByKey("remote-shippingFee").getValue());
        String remoteAreas = parametersService.getParameterByKey("remote-areas").getValue();

        if(cart == null)
            return 0d;

        if(cart.getShippingAddress() == null)
            return 0d;

        if(remoteAreas.contains(cart.getShippingAddress().getProvince()))
            return remoteShippingFee;

        return shippingFee;
    }

}
